

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class Audio {

  private static final int EXTERNAL_BUFFER_SIZE = 128000;

  public static void start(InputStream soundInput) {
    //System.out.println("Audio.start: " + soundFile.getAbsolutePath());
    //File soundFile = new File(snd);

    AudioInputStream audioInputStream = null;
    try {
      //audioInputStream = AudioSystem.getAudioInputStream(soundFile);
      audioInputStream = AudioSystem.getAudioInputStream(soundInput);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }

    AudioFormat audioFormat = audioInputStream.getFormat();
    SourceDataLine line = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try {
      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(audioFormat);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      // System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }
    line.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
    while (nBytesRead != -1) {
      try {
        nBytesRead = audioInputStream.read(abData, 0, abData.length);
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (nBytesRead >= 0) {
        int nBytesWritten = line.write(abData, 0, nBytesRead);
      }
    }

    line.drain();
    line.close();
    // System.exit(0);
  }





  public static void start(URL deURL) {
    System.out.println("Audio.start: " + deURL);
    //File soundFile = new File(snd);

    AudioInputStream audioInputStream = null;
    try {
      //audioInputStream = AudioSystem.getAudioInputStream(soundFile);
      //audioInputStream = AudioSystem.getAudioInputStream(soundInput);
      audioInputStream = AudioSystem.getAudioInputStream(deURL);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }

    AudioFormat audioFormat = audioInputStream.getFormat();
    SourceDataLine line = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try {
      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(audioFormat);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      // System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }
    line.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
    while (nBytesRead != -1) {
      try {
        nBytesRead = audioInputStream.read(abData, 0, abData.length);
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (nBytesRead >= 0) {
        int nBytesWritten = line.write(abData, 0, nBytesRead);
      }
    }

    line.drain();
    line.close();
    // System.exit(0);
  }


  public static void start(String sndStr) {
    System.out.println("Audio.start: " + sndStr);
    File soundFile = new File(sndStr);

    AudioInputStream audioInputStream = null;
    try {
      //audioInputStream = AudioSystem.getAudioInputStream(soundFile);
      audioInputStream = AudioSystem.getAudioInputStream(soundFile);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }

    AudioFormat audioFormat = audioInputStream.getFormat();
    SourceDataLine line = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try {
      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(audioFormat);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      // System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }
    line.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
    while (nBytesRead != -1) {
      try {
        nBytesRead = audioInputStream.read(abData, 0, abData.length);
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (nBytesRead >= 0) {
        int nBytesWritten = line.write(abData, 0, nBytesRead);
      }
    }

    line.drain();
    line.close();
    // System.exit(0);
  }


  public static void start(File snd) {
  System.out.println("Audio.start: " + snd.getAbsolutePath());
   // File soundFile = new File(snd);

    AudioInputStream audioInputStream = null;
    try {
      //audioInputStream = AudioSystem.getAudioInputStream(soundFile);
      audioInputStream = AudioSystem.getAudioInputStream(snd);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }

    AudioFormat audioFormat = audioInputStream.getFormat();
    SourceDataLine line = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try {
      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(audioFormat);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      // System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }
    line.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
    while (nBytesRead != -1) {
      try {
        nBytesRead = audioInputStream.read(abData, 0, abData.length);
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (nBytesRead >= 0) {
        int nBytesWritten = line.write(abData, 0, nBytesRead);
      }
    }

    line.drain();
    line.close();
    // System.exit(0);
  }


  public static void start(URI snd) {
  System.out.println("Audio.start: " + snd);
    File soundFile = new File(snd);

    AudioInputStream audioInputStream = null;
    try {
      audioInputStream = AudioSystem.getAudioInputStream(soundFile);
      //audioInputStream = AudioSystem.getAudioInputStream(snd);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }

    AudioFormat audioFormat = audioInputStream.getFormat();
    SourceDataLine line = null;
    DataLine.Info info = new DataLine.Info(SourceDataLine.class,
            audioFormat);
    try {
      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(audioFormat);
    } catch (LineUnavailableException e) {
      e.printStackTrace();
      // System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
      // System.exit(1);
    }
    line.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
    while (nBytesRead != -1) {
      try {
        nBytesRead = audioInputStream.read(abData, 0, abData.length);
      } catch (IOException e) {
        e.printStackTrace();
      }
      if (nBytesRead >= 0) {
        int nBytesWritten = line.write(abData, 0, nBytesRead);
      }
    }

    line.drain();
    line.close();
    // System.exit(0);
  }

}
